package guestbook;
import javax.servlet.http.*;

/*
 * this class will run based on the cron jobs. it fetches the totoResult information
 * and stores it in the DB
 */

public class gettingTotoResult extends HttpServlet 
{    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	 {
		try 
		{
			
			String url="http://www.dapper.net/RunDapp?dappName=TotoResults_totoSite_221209&v=1&applyToUrl=http%3A%2F%2Fwww.toto.org.il%2Ftotodata%2Fwins.asp";
			String field=null;
			String[] result=null;
			String[] signs= new String[16];
			String[] winnings= new String[5];
			boolean flag=true;
		
			totoResult form=new totoResult(signs,winnings);
			
			/* this loop gets the data for all relevant fields*/
			for (int i=1;i<3;i++){
				switch (i) {
					case 1: field="Results";break;
					case 2: field="Winnings";break;
				}
			result=parsingSites.getDataFromXml(result, field, url);
			/* if the result is null, it means a dapper approach failed*/
			
				if (result==null){
					resp.getWriter().println("error");
					flag=false;
					return;
				}			
			
				switch (i) {
					case 1: form.setSigns(result);break;
					case 2: form.setWinnings(result);break;
				}
			/* when the code above is complete, we have a form object ready for DB*/
			}
			
			
				
		/* the code below stores the form object in the DB, for quicker approach
		  if the flag is true, it means result was never null*/
			
			DBMethods.saveInDB(form,flag);
			
			/* returns success to caller if all is well*/
			resp.setContentType("text/plain;charset=UTF-8");
			resp.getWriter().println("success");
		
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
			
}
